<template>
  <div>
    <van-nav-bar title="挖金社区-登录" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="user.mobile"
        name="mobile"
        label="手机号"
        required
        placeholder="请输入11位手机号"
        :rules="[
          { required: true, pattern: /^1[3-9]\d{9}$/, message: '请填写手机号' },
        ]"
      />
      <van-field
        v-model="user.code"
        type="password"
        name="code"
        label="密码"
        required
        placeholder="请输入6位密码"
        :rules="[{ required: true, pattern: /^\d{6}$/, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button
          :loading="isLoading"
          :disabled="isLoading"
          loading-text="玩命加载中..."
          round
          block
          type="info"
          native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { loginAPI } from "@/api";
import { MyNotify } from "@/utils/Notify";
import { setToken } from "@/utils/token";
import { setStorage } from "@/utils/storage";
export default {
  data() {
    return {
      user: {
        mobile: "", //手机号
        code: "", //密码(后台规定无论手机号是多少，密码都是246810)
      },
      isLoading: false, //当前是否正在加载
    };
  },
  methods: {
    async onSubmit(values) {
      this.isLoading = true;
      //发送请求
      try {
        const res = await loginAPI(values);
        // console.log(res);
        MyNotify({ type: "success", message: "登录成功" });
        //保存token值
        setToken(res.data.data.token);
        //保存无感知刷新的token
        setStorage("refresh_token", res.data.data.refresh_token);
        //登录成功进行跳转
        //location.href 当前路径和需要跳转的路径一致（不包括锚点后的路径）,则不会刷新跳转
        //this.$router.push() 该方法会产生压栈，跳转后可以回退
        this.$router.replace({
          path: this.$route.query.path || "/layout/home",
        });
      } catch (error) {
        MyNotify({ type: "warning", message: "账号或密码错误" });
      }
      this.isLoading = false;
    },
  },
};
</script>

<style scoped lang="less">
/*此选择器名字是组件的根标签*/
// .van-nav-bar {
//   background-color: #007bff;
// }
/*
如果需要修改组件内的样式就需要使用/deep/
/deep/就是把data-v-hash值选择器写到类名的前面 */
// /deep/ .van-nav-bar__title {
//   color: white;
// }
</style>
